package com.leon.datalink.web.testcontroller;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.leon.datalink.core.utils.JacksonUtils;
import org.springframework.web.bind.annotation.*;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.OperatingSystemMXBean;
import java.lang.management.RuntimeMXBean;
import java.util.Random;

/**
 * System monitoring controller for development - simplified version without complex dependencies
 */
@RestController
@RequestMapping("/api/system")
@CrossOrigin(origins = "*", maxAge = 3600)
public class SystemMonitorController {

    private final Random random = new Random();

    @GetMapping("/info")
    public Object getSystemInfo() {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("code", 200);
        result.put("message", "success");
        
        ObjectNode data = JacksonUtils.createEmptyJsonNode();
        
        // 获取系统信息
        RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
        OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
        
        data.put("localMemberName", "datalink-node-1");
        data.put("version", "1.0.0");
        data.put("time", java.time.LocalDateTime.now().toString());
        data.put("username", "admin");
        data.put("javaVersion", System.getProperty("java.version"));
        data.put("osName", System.getProperty("os.name"));
        data.put("availableProcessors", osBean.getAvailableProcessors());
        data.put("uptime", runtimeBean.getUptime());
        
        result.set("data", data);
        return result;
    }

    @GetMapping("/statistics")
    public Object getSystemStatistics() {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("code", 200);
        result.put("message", "success");
        
        ObjectNode data = JacksonUtils.createEmptyJsonNode();
        
        // 模拟统计数据
        data.put("resourceCount", random.nextInt(50) + 10);
        data.put("ruleCount", random.nextInt(20) + 5);
        data.put("activeConnections", random.nextInt(10) + 1);
        data.put("totalRequests", random.nextInt(10000) + 1000);
        
        result.set("data", data);
        return result;
    }

    @GetMapping("/disk")
    public Object getDiskInfo() {
        ObjectNode result = JacksonUtils.createEmptyJsonNode();
        result.put("code", 200);
        result.put("message", "success");

        // 模拟磁盘信息数组
        ArrayNode diskArray = JacksonUtils.createEmptyArrayNode();

        ObjectNode diskInfo = JacksonUtils.createEmptyJsonNode();
        diskInfo.put("name", "System Disk");
        diskInfo.put("total", 1000000000000L); // 1TB
        diskInfo.put("free", 500000000000L);   // 500GB
        diskInfo.put("used", 500000000000L);   // 500GB
        diskArray.add(diskInfo);

        // 添加第二个磁盘
        ObjectNode diskInfo2 = JacksonUtils.createEmptyJsonNode();
        diskInfo2.put("name", "Data Disk");
        diskInfo2.put("total", 2000000000000L); // 2TB
        diskInfo2.put("free", 1200000000000L);  // 1.2TB
        diskInfo2.put("used", 800000000000L);   // 800GB
        diskArray.add(diskInfo2);

        result.set("data", diskArray);
        return result;
    }
}
